﻿using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Common
{
    public  class MD5Helper
    {
        /// <summary>
        /// MD5加密  32位
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string CreateMD5(string value)
        {
            using MD5 mi = MD5.Create();
            byte[] buffer = Encoding.Default.GetBytes(value);

            //开始加密

            byte[] newBuffer = mi.ComputeHash(buffer);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < newBuffer.Length; i++)

            {

                sb.Append(newBuffer[i].ToString("x2"));

            }

            return sb.ToString();
        }
        /// <summary>
        /// MD532位加密    流形式
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string CreateMD5(Stream stream)
        {
            using MD5 mi = MD5.Create();
            //开始加密
            byte[] newBuffer = mi.ComputeHash(stream);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < newBuffer.Length; i++)

            {

                sb.Append(newBuffer[i].ToString("x2"));

            }

            return sb.ToString();
        }

    }
}
